
var delimiter={};
var column_example={'检索号':'12345678','原文语言':'中文(英文)','发表时间':'2015年5月4日','卷':'第1卷(Vol:1)','期':'第2期(No:2)','页码':'pp:12-34'

};
var nowSelected='N';//指示现在选择的导出选项
var addedRow=0;

function previewRecord(oRight)
{
	var preview="<b>导出预览:</b>";
	for(var i=0;i<oRight.length;++i)
	{
		var title_name=column_example[oRight.options[i].value]?column_example[oRight.options[i].value]:oRight.options[i].value;
		preview+=title_name+delimiter[oRight.options[i].value];//生成预览字符串
	}
	var submit_id=document.getElementById('submitExportItems');
	submit_id.rows[2].cells[0].innerHTML=preview;
}

/*移除左边选中的列表项到右边*/ 
function fMoveSelectedOptionsLeftToRight(oLeft,oRight) 
{ 
	if(!(oLeft&&oRight)) 
	{ 
		return; 
	} 
	if(!hasOptions(oLeft)) 
	{ 
		return; 
	} 
	if(oLeft.selectedIndex==-1) 
	{ 
		oLeft.selectedIndex=0; 
	} 
	var del_id=document.getElementById('exportdelimiter');
	
	for(var i=0;i<oLeft.options.length;i++) 
	{ 
		if(oLeft.options[i].selected) 
		{  
			var opt = document.createElement ("option");
			if(del_id){
				var del_index=del_id.selectedIndex;//选中的索引
				delimiter[oLeft.options[i].value]=del_id.options[del_index].value;//获取值
			}
			opt.value = oLeft.options[i].value;
			opt.innerText = oLeft.options[i].text;
			oRight.appendChild (opt);
		} 
	} 
	if(del_id)
	previewRecord(oRight);
	clearSelectedOptions(oLeft); 
} 
/*移除左边的所有列表项到右边*/ 
function fMoveAllOptionsLeftToRight(oLeft,oRight) 
{ 
	if(!(oLeft&&oRight)) 
	{ 
		
		return; 
	} 
	if(!hasOptions(oLeft)) 
	{ 
		
		return; 
	} 
	var del_id=document.getElementById('exportdelimiter');
	for(var i=0;i<oLeft.options.length;i++) 
	{ 
	var opt = document.createElement ("option");
	if(del_id){
		var del_index=del_id.selectedIndex;//选中的索引
		delimiter[oLeft.options[i].value]=del_id.options[del_index].value;//获取值
	}
		opt.value = oLeft.options[i].value;
		opt.innerText = oLeft.options[i].text;
		oRight.appendChild (opt);
	} 
	clearAllOptions(oLeft); 
	if(del_id)
	previewRecord(oRight);
} 
/*移除右边选中的列表项到左边*/ 
function fMoveSelectedOptionsRightToLeft(oLeft,oRight) 
{ 
	if(!(oLeft&&oRight)) 
	{ 
		return; 
	} 
	if(!hasOptions(oRight)) 
	{ 
		return; 
	} 
	if(oRight.selectedIndex==-1) 
	{ 
		oRight.selectedIndex=0; 
	} 
	var del_id=document.getElementById('exportdelimiter');
	for(var i=0;i<oRight.options.length;i++) 
	{ 
		if(oRight.options[i].selected) 
		{ 
			var opt=document.createElement("OPTION");
			if(del_id){
				var del_index=del_id.selectedIndex;//选中的索引
				delimiter[oRight.options[i].value]="";//取消分隔符
			}
			opt.value = oRight.options[i].value;
			opt.innerText = oRight.options[i].text;
			oLeft.appendChild(opt);
			
		} 
	} 
	clearSelectedOptions(oRight); 
	if(del_id)
	previewRecord(oRight);
} 
/*移除右边的所有列表项到左边*/ 
function fMoveAllOptionsRightToLeft(oLeft,oRight) 
{ 
	if(!(oLeft&&oRight)) 
	{ 
		return; 
	} 
	if(!hasOptions(oRight)) 
	{ 
		return; 
	} 
	var del_id=document.getElementById('exportdelimiter');
	for(var i=0;i<oRight.options.length;i++) 
	{ 
	var opt=document.createElement("OPTION");
	if(del_id)
		{
			var del_index=del_id.selectedIndex;//选中的索引
		delimiter[oRight.options[i].value]="";//取消分隔符
		}
		opt.value = oRight.options[i].value;
		opt.innerText = oRight.options[i].text;
		oLeft.appendChild(opt);
	} 
	
	clearAllOptions(oRight); 
	if(del_id)
	previewRecord(oRight);
} 
/*清空select所有options*/ 
function clearAllOptions(oSelect) 
{ 
	if(oSelect) 
	{ 
		var ops=oSelect.options; 
		while(ops.length>0) 
		{ 
			oSelect.remove(ops.length-1); 
		} 
	} 
} 
/*清空select所有选中的options*/ 
function clearSelectedOptions(oSelect) 
{ 
	if(oSelect) 
	{ 
		for(var i=0;i<oSelect.options.length;i++) 
		{ 
			if(oSelect.options[i].selected) 
			{ 
				oSelect.remove(i--); 
			} 
		} 
	} 
} 
/*判断select是否有options*/ 
function hasOptions(oSelect) 
{ 
	if(oSelect) 
	{ 
		return oSelect.options.length>0; 
	} 
	return false; 
} 
function checkForm()
{ 
	var onRight=document.getElementById("rightExportItems");//forms[0].rightItem; 
	alert(onRight.options.length); 
	for(var i=0;i<onRight.options.length;i++){ 
		var text=onRight.options[i].text; 
		var value=onRight.options[i].value; 
		alert(value+" "+text); 
	} 
}

function getExportItems()
{
	//获取右边select的option
	var str="";
	var del="";
	var onRight=document.getElementById("rightExportItems");//forms[0].rightItem; 
	if(onRight.options.length==0){
		alert("请至少选择一个导出表项");
		return;
	}
	for(var i=0;i<onRight.options.length;i++){ 
		str+=(str=="")?onRight.options[i].value:("__"+onRight.options[i].value); 
		del+=(del=="")?delimiter[onRight.options[i].value]:("__"+delimiter[onRight.options[i].value]);
		//alert(onRight.options[i].value+" "+delimiter[onRight.options[i].value]);
	} 
	//隐藏在标签里传递导出列
	var submit_id=document.getElementById('submitExportItems');
	var new_row;
	
	//隐藏在标签里传递分隔符
	if(document.getElementById('exportdelimiter')){
		if(submit_id.rows.length==6){//删除以前的input
			submit_id.deleteRow(5);
			submit_id.deleteRow(4);
		}
		else
			addedRow+=2;
		new_row=submit_id.insertRow(4);
		column=new_row.insertCell(0);
		column.innerHTML="<td><input type='hidden' name='exportItems' value='"+str+"'></td>";
		new_row=submit_id.insertRow(5);
		column=new_row.insertCell(0);
		column.innerHTML="<td><input type='hidden' name='exportDels' value='"+del+"'></td>";
	}else{
		if(submit_id.rows.length==3){
			submit_id.deleteRow(2);
		}
		else
		addedRow+=1;
		new_row=submit_id.insertRow(2);
		column=new_row.insertCell(0);
		column.innerHTML="<td><input type='hidden' name='exportItems' value='"+str+"'></td>";
	}

}

function createInputWord()
{
	var submit_id=document.getElementById('submitExportItems');
	//选择分隔符
	var new_row=submit_id.insertRow(0);
	column=new_row.insertCell(0);
	column.innerHTML="<td>选择分隔符:<select name='delimiter' id='exportdelimiter'><option value='.'>.</option><option value=','>,</option>"
	+"<option value='#'>#</option><option value=':'>:</option><option value=']'>]</option><option value='['>[</option>"
	+"<option value='\"'>\"</option><option value=\"'\">'</option><option value='('>(</option><option value=')'>)</option></select></td>";
	//导出类型
	new_row=submit_id.insertRow(1);
	column=new_row.insertCell(0);
	column.innerHTML="<td><input type='hidden' name='export_type' value='word'></td>";
	//预览
	new_row=submit_id.insertRow(2);
	column=new_row.insertCell(0);
	column.innerHTML="<td></td> ";
	//submit_id.rows[2].cells[0].innerText='预览';
	new_row=submit_id.insertRow(3);
	column=new_row.insertCell(0);
	column.innerHTML="<td><input type='submit' value='导出' onclick='getExportItems()'/></td> ";
	
	
}

function createInputExcel()
{
	var submit_id=document.getElementById('submitExportItems');
	var new_row=submit_id.insertRow(0);
	column=new_row.insertCell(0);
	column.innerHTML="<input type='hidden' name='export_type' value='excel'>";
	new_row=submit_id.insertRow(1);
	
	new_row.insertCell(0).innerHTML="　　<label style='display:inline;' title='勾选此项后，第一作者，第二作者...通信作者和其他作者会合并成一列，以逗号隔开'><input type='checkbox' name='merge_all_authors' value='merge'> 作者合为一列</label>";
	new_row.insertCell(1).innerHTML="<input type='submit' value='导出' onclick='getExportItems()'/>";
	new_row.cells[0].style.width="200px";
	new_row.style.height="30px";
	new_row.style.vAlign="bottom";
	//new_row.cells[1].style.vAlign="middle";
}


function showExportWordChoices()
{
	var left_item=document.getElementById('leftExportItems'),right_item=document.getElementById('rightExportItems');
	
	(document.getElementById('exportWord').style.backgroundColor=='#EEB4B4')?(document.getElementById('exportWord').style.backgroundColor=''):
	(document.getElementById('exportWord').style.backgroundColor='#EEB4B4');
	document.getElementById('exportExcel').style.backgroundColor="";
	//alert('111');
	switch(nowSelected)	
	{
		case 'N':
		document.getElementById('exportItems').style.display = 'block';
		createInputWord();
		addedRow+=4;
		nowSelected='W';
		break;
		case 'W':
		while(addedRow--)
		{
			
			document.getElementById('submitExportItems').deleteRow();
		}
		addedRow=0;
		fMoveAllOptionsRightToLeft(left_item,right_item);//隐藏的时候所有的选项复位
		document.getElementById('exportItems').style.display = 'none';
		nowSelected='N';
		break;
		case 'E':
		while(addedRow--)
		{
			
			document.getElementById('submitExportItems').deleteRow();
		}
		addedRow=0;
		fMoveAllOptionsRightToLeft(left_item,right_item);//从右跳到左的时候所有的选项复位
		createInputWord();
		addedRow+=4;
		nowSelected='W';
		break;
		default:break;
	}
	
}

function showExportExcelChoices()
{
	var left_item=document.getElementById('leftExportItems'),right_item=document.getElementById('rightExportItems');
	(document.getElementById('exportExcel').style.backgroundColor=='#EEB4B4')?(document.getElementById('exportExce;').style.backgroundColor=''):
	(document.getElementById('exportExcel').style.backgroundColor='#EEB4B4');
	document.getElementById('exportWord').style.backgroundColor="";
	switch(nowSelected)	
	{
		case 'N':
		document.getElementById('exportItems').style.display = 'block';
		createInputExcel();
		addedRow+=2;
		nowSelected='E';
		break;
		case 'W':
		while(addedRow--)
			document.getElementById('submitExportItems').deleteRow();
		addedRow=0;
		fMoveAllOptionsRightToLeft(left_item,right_item);//从右跳到左的时候所有的选项复位
		createInputExcel();
		addedRow+=2;
		nowSelected='E';
		break;
		case 'E':
		while(addedRow--)
		{
			
			document.getElementById('submitExportItems').deleteRow();
		}
		addedRow=0;
		fMoveAllOptionsRightToLeft(left_item,right_item);//从右跳到左的时候所有的选项复位
		document.getElementById('exportItems').style.display = 'none';
		nowSelected='N';
		break;
		default:break;
	}
	
	 
}

var x=null;
//鼠标按下不放时的操作
function setTimeStart(type)
{
	
	//超过0.3秒启动连续的向上(下)的操作
	if(type=="up")
	{
		x=setTimeout(upListItem,300);
	}else
	{
		x=setTimeout(downListItem,300);
	}
}
//将选中item向上
function upListItem()
{
   var listObj=document.getElementById('rightExportItems');
   var i=0;
   var choosed=0;
   var sel_indexs=new Array();
   for(;i<listObj.length;++i){
	   if(listObj.options[i].selected){
		sel_indexs[choosed]=i;
		choosed+=1;
	  }
   }
   if(!choosed){
	   if(x!=null){clearTimeout(x);}
	   //alert("请选择一项");
	   return;
   }
   var selIndex;
   var fir_sel=sel_indexs[0];
   for(i=0;i<sel_indexs.length;++i){
	   selIndex=sel_indexs[i];
	   if(selIndex<=0){
		   //alert("已经移到顶端了");
		   if(x!=null){clearTimeout(x);}
		   return;
	   }
		var selValue=listObj.options[selIndex].value;
		var selText=listObj.options[selIndex].text;
		listObj.options[selIndex].value=listObj.options[selIndex-1].value;
		listObj.options[selIndex].text=listObj.options[selIndex-1].text;
		listObj.options[selIndex].selected=false;
		listObj.options[selIndex-1].value=selValue;
		listObj.options[selIndex-1].text=selText;
		listObj.options[selIndex-1].selected=true;
  }
  //预览
   var del_id=document.getElementById('exportdelimiter');
   if(del_id)
	   previewRecord(listObj);
   if(fir_sel>0)
   {
	 x=setTimeout(upListItem,200);
   }
}
//将选中item向下
function downListItem()
{
   var listObj=document.getElementById('rightExportItems');
   var i=0;
   var choosed=0;
   var sel_indexs=new Array();
   for(;i<listObj.length;++i){
	   if(listObj.options[i].selected){
		sel_indexs[choosed]=i;
		choosed+=1;
	  }
   }
    if(!choosed){
	   if(x!=null){clearTimeout(x);}
	   //alert("请选择一项");
	   return;
   }
   var selIndex;
   
   for(i=sel_indexs.length-1;i>=0;--i){
	    selIndex=sel_indexs[i];
		if(selIndex==listObj.options.length-1){
			if(x!=null){clearTimeout(x);}
			//alert("已经移到最后一位！");
			return;
		}
		var selValue=listObj.options[selIndex].value;
		var selText=listObj.options[selIndex].text;
		listObj.options[selIndex].value=listObj.options[selIndex+1].value;
		listObj.options[selIndex].text=listObj.options[selIndex+1].text;
		listObj.options[selIndex].selected=false;
		listObj.options[selIndex+1].value=selValue;
		listObj.options[selIndex+1].text=selText;
		listObj.options[selIndex+1].selected=true;
		
   }
   //预览
   var del_id=document.getElementById('exportdelimiter');
   if(del_id)
	   previewRecord(listObj);
   var last_sel=sel_indexs[sel_indexs.length-1];
   if(last_sel<listObj.options.length-1)
   {
	 x=setTimeout(downListItem,200);
   }
} 